# SPDX-License-Identifier: Apache-2.0
"""
add_billing_and_subscriptions

Revision ID: fe2e3d22b3fa
Revises: 7eaad728b806
Create Date: 2022-09-08 21:34:59.975082
"""

import sqlalchemy as sa

from alembic import op
from sqlalchemy.dialects import postgresql

revision = "fe2e3d22b3fa"
down_revision = "7eaad728b806"

# Note: It is VERY important to ensure that a migration does not lock for a
#       long period of time and to ensure that each individual migration does
#       not break compatibility with the *previous* version of the code base.
#       This is because the migrations will be ran automatically as part of the
#       deployment process, but while the previous version of the code is still
#       up and running. Thus backwards incompatible changes must be broken up
#       over multiple migrations inside of multiple pull requests in order to
#       phase them in over multiple deploys.
#
#       By default, migrations cannot wait more than 4s on acquiring a lock
#       and each individual statement cannot take more than 5s. This helps
#       prevent situations where a slow migration takes the entire site down.
#
#       If you need to increase this timeout for a migration, you can do so
#       by adding:
#
#           op.execute("SET statement_timeout = 5000")
#           op.execute("SET lock_timeout = 4000")
#
#       To whatever values are reasonable for this migration as part of your
#       migration.


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "stripe_customers",
        sa.Column(
            "id",
            postgresql.UUID(as_uuid=True),
            server_default=sa.text("gen_random_uuid()"),
            nullable=False,
        ),
        sa.Column("customer_id", sa.Text(), nullable=False),
        sa.Column("billing_email", sa.Text(), nullable=True),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("customer_id"),
    )
    op.create_table(
        "stripe_subscription_products",
        sa.Column(
            "id",
            postgresql.UUID(as_uuid=True),
            server_default=sa.text("gen_random_uuid()"),
            nullable=False,
        ),
        sa.Column("product_id", sa.Text(), nullable=True),
        sa.Column("product_name", sa.Text(), nullable=False),
        sa.Column("description", sa.Text(), nullable=False),
        sa.Column(
            "is_active", sa.Boolean(), server_default=sa.text("true"), nullable=False
        ),
        sa.Column("tax_code", sa.Text(), nullable=True),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "organization_projects",
        sa.Column(
            "id",
            postgresql.UUID(as_uuid=True),
            server_default=sa.text("gen_random_uuid()"),
            nullable=False,
        ),
        sa.Column("organization_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("project_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.ForeignKeyConstraint(
            ["organization_id"],
            ["organizations.id"],
            onupdate="CASCADE",
            ondelete="CASCADE",
        ),
        sa.ForeignKeyConstraint(
            ["project_id"], ["projects.id"], onupdate="CASCADE", ondelete="CASCADE"
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint(
            "organization_id",
            "project_id",
            name="_organization_projects_organization_project_uc",
        ),
    )
    op.create_index(
        "organization_projects_organization_id_idx",
        "organization_projects",
        ["organization_id"],
        unique=False,
    )
    op.create_index(
        "organization_projects_project_id_idx",
        "organization_projects",
        ["project_id"],
        unique=False,
    )
    op.create_table(
        "organization_stripe_customers",
        sa.Column(
            "id",
            postgresql.UUID(as_uuid=True),
            server_default=sa.text("gen_random_uuid()"),
            nullable=False,
        ),
        sa.Column("organization_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("stripe_customer_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.ForeignKeyConstraint(
            ["organization_id"],
            ["organizations.id"],
            onupdate="CASCADE",
            ondelete="CASCADE",
        ),
        sa.ForeignKeyConstraint(
            ["stripe_customer_id"],
            ["stripe_customers.id"],
            onupdate="CASCADE",
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint(
            "organization_id",
            "stripe_customer_id",
            name="_organization_stripe_customers_organization_customer_uc",
        ),
    )
    op.create_index(
        "organization_stripe_customers_organization_id_idx",
        "organization_stripe_customers",
        ["organization_id"],
        unique=False,
    )
    op.create_index(
        "organization_stripe_customers_stripe_customer_id_idx",
        "organization_stripe_customers",
        ["stripe_customer_id"],
        unique=False,
    )
    op.create_table(
        "stripe_subscription_prices",
        sa.Column(
            "id",
            postgresql.UUID(as_uuid=True),
            server_default=sa.text("gen_random_uuid()"),
            nullable=False,
        ),
        sa.Column("price_id", sa.Text(), nullable=True),
        sa.Column("currency", sa.Text(), nullable=False),
        sa.Column(
            "subscription_product_id", postgresql.UUID(as_uuid=True), nullable=False
        ),
        sa.Column("unit_amount", sa.Integer(), nullable=False),
        sa.Column(
            "is_active", sa.Boolean(), server_default=sa.text("true"), nullable=False
        ),
        sa.Column(
            "recurring",
            sa.Enum(
                "month", "year", "week", "day", name="stripesubscriptionpriceinterval"
            ),
            nullable=False,
        ),
        sa.Column("tax_behavior", sa.Text(), nullable=True),
        sa.ForeignKeyConstraint(
            ["subscription_product_id"],
            ["stripe_subscription_products.id"],
            onupdate="CASCADE",
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "stripe_subscriptions",
        sa.Column(
            "id",
            postgresql.UUID(as_uuid=True),
            server_default=sa.text("gen_random_uuid()"),
            nullable=False,
        ),
        sa.Column("stripe_customer_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("subscription_id", sa.Text(), nullable=False),
        sa.Column(
            "subscription_price_id", postgresql.UUID(as_uuid=True), nullable=False
        ),
        sa.Column(
            "status",
            sa.Enum(
                "active",
                "past_due",
                "unpaid",
                "canceled",
                "incomplete",
                "incomplete_expired",
                "trialing",
                name="stripesubscriptionstatus",
            ),
            nullable=False,
        ),
        sa.ForeignKeyConstraint(
            ["stripe_customer_id"],
            ["stripe_customers.id"],
            onupdate="CASCADE",
            ondelete="CASCADE",
        ),
        sa.ForeignKeyConstraint(
            ["subscription_price_id"],
            ["stripe_subscription_prices.id"],
            onupdate="CASCADE",
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint(
            "stripe_customer_id",
            "subscription_id",
            name="_stripe_subscription_customer_subscription_uc",
        ),
    )
    op.create_index(
        "stripe_subscriptions_stripe_customer_id_idx",
        "stripe_subscriptions",
        ["stripe_customer_id"],
        unique=False,
    )
    op.create_index(
        "stripe_subscriptions_subscription_id_idx",
        "stripe_subscriptions",
        ["subscription_id"],
        unique=False,
    )
    op.create_table(
        "organization_stripe_subscriptions",
        sa.Column(
            "id",
            postgresql.UUID(as_uuid=True),
            server_default=sa.text("gen_random_uuid()"),
            nullable=False,
        ),
        sa.Column("organization_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("subscription_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.ForeignKeyConstraint(
            ["organization_id"],
            ["organizations.id"],
            onupdate="CASCADE",
            ondelete="CASCADE",
        ),
        sa.ForeignKeyConstraint(
            ["subscription_id"],
            ["stripe_subscriptions.id"],
            onupdate="CASCADE",
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint(
            "organization_id",
            "subscription_id",
            name="_organization_stripe_subscriptions_organization_subscription_uc",
        ),
    )
    op.create_index(
        "organization_stripe_subscriptions_organization_id_idx",
        "organization_stripe_subscriptions",
        ["organization_id"],
        unique=False,
    )
    op.create_index(
        "organization_stripe_subscriptions_subscription_id_idx",
        "organization_stripe_subscriptions",
        ["subscription_id"],
        unique=False,
    )
    op.create_table(
        "stripe_subscription_items",
        sa.Column(
            "id",
            postgresql.UUID(as_uuid=True),
            server_default=sa.text("gen_random_uuid()"),
            nullable=False,
        ),
        sa.Column("subscription_item_id", sa.Text(), nullable=True),
        sa.Column("subscription_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column(
            "subscription_price_id", postgresql.UUID(as_uuid=True), nullable=False
        ),
        sa.Column("quantity", sa.Integer(), nullable=False),
        sa.ForeignKeyConstraint(
            ["subscription_id"],
            ["stripe_subscriptions.id"],
            onupdate="CASCADE",
            ondelete="CASCADE",
        ),
        sa.ForeignKeyConstraint(
            ["subscription_price_id"],
            ["stripe_subscription_prices.id"],
            onupdate="CASCADE",
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.drop_index(
        "organization_project_organization_id_idx", table_name="organization_project"
    )
    op.drop_index(
        "organization_project_project_id_idx", table_name="organization_project"
    )
    op.drop_table("organization_project")
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "organization_project",
        sa.Column(
            "id",
            postgresql.UUID(),
            server_default=sa.text("gen_random_uuid()"),
            autoincrement=False,
            nullable=False,
        ),
        sa.Column(
            "organization_id", postgresql.UUID(), autoincrement=False, nullable=False
        ),
        sa.Column("project_id", postgresql.UUID(), autoincrement=False, nullable=False),
        sa.ForeignKeyConstraint(
            ["organization_id"],
            ["organizations.id"],
            name="organization_project_organization_id_fkey",
            onupdate="CASCADE",
            ondelete="CASCADE",
        ),
        sa.ForeignKeyConstraint(
            ["project_id"],
            ["projects.id"],
            name="organization_project_project_id_fkey",
            onupdate="CASCADE",
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint("id", name="organization_project_pkey"),
        sa.UniqueConstraint(
            "organization_id",
            "project_id",
            name="_organization_project_organization_project_uc",
        ),
    )
    op.create_index(
        "organization_project_project_id_idx",
        "organization_project",
        ["project_id"],
        unique=False,
    )
    op.create_index(
        "organization_project_organization_id_idx",
        "organization_project",
        ["organization_id"],
        unique=False,
    )
    op.drop_table("stripe_subscription_items")
    op.drop_index(
        "organization_stripe_subscriptions_subscription_id_idx",
        table_name="organization_stripe_subscriptions",
    )
    op.drop_index(
        "organization_stripe_subscriptions_organization_id_idx",
        table_name="organization_stripe_subscriptions",
    )
    op.drop_table("organization_stripe_subscriptions")
    op.drop_index(
        "stripe_subscriptions_subscription_id_idx", table_name="stripe_subscriptions"
    )
    op.drop_index(
        "stripe_subscriptions_stripe_customer_id_idx", table_name="stripe_subscriptions"
    )
    op.drop_table("stripe_subscriptions")
    op.drop_table("stripe_subscription_prices")
    op.drop_index(
        "organization_stripe_customers_stripe_customer_id_idx",
        table_name="organization_stripe_customers",
    )
    op.drop_index(
        "organization_stripe_customers_organization_id_idx",
        table_name="organization_stripe_customers",
    )
    op.drop_table("organization_stripe_customers")
    op.drop_index(
        "organization_projects_project_id_idx", table_name="organization_projects"
    )
    op.drop_index(
        "organization_projects_organization_id_idx", table_name="organization_projects"
    )
    op.drop_table("organization_projects")
    op.drop_table("stripe_subscription_products")
    op.drop_table("stripe_customers")
    # ### end Alembic commands ###
